package com.yeyks.controller;

import com.yeyks.dal.dao.AccountInfoMapper;
import com.yeyks.dal.domain.AccountInfo;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.em.ResponseStatus;
import com.yeyks.service.AccountService;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.model.em.ResponseStatus;
import com.yeyks.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class InternalController {

    @Autowired
    AccountInfoMapper accountInfoMapper;

    @Autowired
    AccountService accountService;

    @ApiIgnore
    @RequestMapping(value = "/ajax/internal/insert_account_raw", method = RequestMethod.GET)
    public ResponseEntity<?> insertAccountRaw(
            HttpServletRequest request, HttpServletResponse response,
            AccountInfo accountInfo
    ){
        AjaxResponseBody responseBody = new AjaxResponseBody();

        try {
            accountInfoMapper.deleteByPhone(accountInfo.getPhone());

            accountInfoMapper.insertReturnId(accountInfo);

            if (accountInfo.getId() == null || accountInfo.getId() == 0) {
                throw new Exception();
            }

            String token = accountService.createToken(accountInfo.getId().toString());

            responseBody.setBody(token);

            responseBody.setCode(ResponseStatus.RS_SUCCESS.getCode());
            responseBody.setMsg(ResponseStatus.RS_SUCCESS.getMsg());
        } catch (Throwable e) {
            responseBody.setCode(ResponseStatus.RS_FAIL.getCode());
            responseBody.setMsg(ResponseStatus.RS_FAIL.getMsg());
        }

        return ResponseEntity.ok(responseBody);
    }

}
